/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.teach.service;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.lczyfz.istep.common.constants.DeleteFlag;
import com.lczyfz.istep.common.constants.DistributeOrNot;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.resource.dao.ResourceCourseDao;
import com.lczyfz.istep.modules.resource.entity.ResourceCourse;
import com.lczyfz.istep.modules.sys.entity.User;
import com.lczyfz.istep.modules.sys.utils.UserUtils;
import com.lczyfz.istep.modules.teach.dao.*;
import com.lczyfz.istep.modules.teach.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;

/**
 * 教学管理Service
 * @author cqh
 * @version 2017-01-12
 */
@Service
@Transactional(readOnly = true)
public class TeachDistributeService extends CrudService<TeachDistributeDao, TeachDistribute> {

	@Autowired
	TeachDistributeDao teachDistributeDao;
	@Autowired
	TeachStudentDao teachStudentDao;
	@Autowired
	TeachDistributeStudentDao teachDistributeStudentDao;
	@Autowired
	TeachDistributeClassDao teachDistributeClassDao;
	@Autowired
	TeachDistributeCoursesystemDao teachDistributeCoursesystemDao;
	@Autowired
	TeachDistributeCtfsetDao teachDistributeCtfsetDao;
	@Autowired
	TeachDistributePaperDao teachDistributePaperDao;
	@Autowired
	TeachCourseCountDao teachCourseCountDao;
	@Autowired
	TeachCtfsetcountDao teachCtfsetcountDao;
	@Autowired
	TeachPapercountDao teachPapercountDao;
	@Autowired
	TeachExamDao teachExamDao;
	@Autowired
	TeachStudentExamDao teachStudentExamDao;
	@Autowired
	ResourceCourseDao resourceCourseDao;
	@Autowired
	TeachStudentService teachStudentService;
	@Autowired
	TeachDistributeCoursesystemService teachDistributeCoursesystemService;
	@Autowired
	TeachDistributeCtfsetService teachDistributeCtfsetService;
	@Autowired
	TeachDistributePaperService teachDistributePaperService;
	@Autowired
	TeachExamService teachExamService;
	@Autowired
	TeachStudentExamService teachStudentExamService;

	public TeachDistribute get(String id) {
		return super.get(id);
	}
	
	public List<TeachDistribute> findList(TeachDistribute teachDistribute) {
		return super.findList(teachDistribute);
	}
	
	public Page<TeachDistribute> findPage(Page<TeachDistribute> page, TeachDistribute teachDistribute) {
		return super.findPage(page, teachDistribute);
	}
	
	@Transactional(readOnly = false)
	public void save(TeachDistribute teachDistribute) {
		super.save(teachDistribute);
	}
	
	@Transactional(readOnly = false)
	public void delete(TeachDistribute teachDistribute) {
		super.delete(teachDistribute);
	}

	//根据id获取单条记录
	public TeachDistribute getSingleRecord(String id) {
		return teachDistributeDao.getSingleRecord(id, DeleteFlag.deleteFlag);
	}

	//查看下发课程体系
	public List<TeachDistribute> viewCourseSystem(String ids) {
		TeachDistribute teachDistribute = new TeachDistribute();
		teachDistribute.setId(ids);
		return dao.viewCourseSystem(teachDistribute);
	}

	//查看下发CTF题目集
	public List<TeachDistribute> viewCTFSet(String ids) {
		TeachDistribute teachDistribute = new TeachDistribute();
		teachDistribute.setId(ids);
		return dao.viewCTFSet(teachDistribute);
	}

	//查看下发试卷
	public List<TeachDistribute> viewPaperName(String ids) {
		TeachDistribute teachDistribute = new TeachDistribute();
		teachDistribute.setId(ids);
		return dao.viewPaperName(teachDistribute);
	}

	//查看下发对象：学生或班级
	public List<TeachDistribute> viewTargetName(String ids) {
		TeachDistribute teachDistribute = new TeachDistribute();
		teachDistribute.setId(ids);
		if(teachDistributeClassDao.getByDistributeId(ids).size() != 0 ){
			return dao.viewTargetNameIsClass(teachDistribute);
		}else {
			return dao.viewTargetNameIsStudent(teachDistribute);
		}
	}

	//我的学习任务列表展示
	public Page<TeachDistribute> findStuLearnList(Page<TeachDistribute> page, TeachDistribute teachDistribute) {
		teachDistribute.setPage(page);
		page.setList(teachDistributeDao.findStuLearnList(teachDistribute));
		return page;
	}

	//	============================================== 删除 ==========================================================

	//删除下发
	@Transactional(readOnly = false)
	public void deleteDistribute(TeachDistribute teachDistribute) {

		//TODO 删除时：对之前下发课程记录减1
		updateCourse(teachDistribute);

		//TODO 删除时：先对之前下发CTF题目集记录减1
		updateCtfSet(teachDistribute);

		//TODO 删除时：先对之前下发试卷记录减1
		updatePaper(teachDistribute);

		//TODO 删除时：删除之前下发学生中间表的记录
		deleteDistributeStudentList(teachDistribute);

		//TODO 删除时：删除之前下发班级中间表的记录
		deleteDistributeClassList(teachDistribute);

		//TODO 删除时：删除之前下发课程体系中间表的记录
		deleteDistributeCourseSystemList(teachDistribute);

		//TODO 删除时：先删除之前下发CTF题目集中间表的记录
		deleteDistributeCtfSetList(teachDistribute);

		//TODO 删除时：先删除之前下发试卷中间表的记录
		deleteDistributePaperList(teachDistribute);

		//TODO 修改时：先删除之前配置考试的记录，再插入
		deleteExamList(teachDistribute);

		//TODO 修改时：先删除之前学生考试的记录，再插入
		deleteStudentExamList(teachDistribute);

		//TODO 删除下发表记录
		teachDistributeDao.deleteDistribute(teachDistribute);
	}

	//删除下发-学生中间表
	@Transactional(readOnly = false)
	public void deleteDistributeStudentList(TeachDistribute teachDistribute) {
		//DistributeStudentList 0 时，为修改操作，则先删除之前插入的记录，再重新插入
		TeachDistributeStudent teachDistributeStudent = new TeachDistributeStudent();
		teachDistributeStudent.setDistributeId(teachDistribute.getId());
		List<TeachDistributeStudent> DistributeStudentList= teachDistributeStudentDao.findListByDistributeId(teachDistributeStudent);
		if(DistributeStudentList.size() != 0){
			logger.debug("==Evan=========先删除-下发学生======================");
			for(int i=0; i<DistributeStudentList.size(); i++){
				teachDistributeStudentDao.deleteDistributeStudent(teachDistribute.getId());
			}
		}
	}

	//删除下发-班级中间表
	@Transactional(readOnly = false)
	public void deleteDistributeClassList(TeachDistribute teachDistribute) {
		//DistributeClassList 0 时，为修改操作，则先删除之前插入的记录，再重新插入
		TeachDistributeClass teachDistributeClass = new TeachDistributeClass();
		teachDistributeClass.setDistributeId(teachDistribute.getId());
		List<TeachDistributeClass> DistributeClassList= teachDistributeClassDao.findListByDistributeId(teachDistributeClass);
		if(DistributeClassList.size() != 0){
			logger.debug("==Evan=========先删除-下发班级======================");
			for(int i=0; i<DistributeClassList.size(); i++){
				teachDistributeClassDao.deleteDistributeClass(teachDistribute.getId());
			}
		}
	}

	//删除下发-课程体系中间表
	@Transactional(readOnly = false)
	public void deleteDistributeCourseSystemList(TeachDistribute teachDistribute) {
		//当CoursesystemList不为 0 时，为修改操作，则先删除之前插入的记录，再重新插入
		TeachDistributeCoursesystem teachDistributeCoursesystem = new TeachDistributeCoursesystem();
		teachDistributeCoursesystem.setDistributeId(teachDistribute.getId());
		List<TeachDistributeCoursesystem> CoursesystemList = teachDistributeCoursesystemService.findList(teachDistributeCoursesystem);
		if(CoursesystemList.size() != 0){
			logger.debug("==Evan=========先删除-课程体系======================");
			for(int i=0; i<CoursesystemList.size(); i++){
				teachDistributeCoursesystemDao.deleteDistributeCoursesystem(teachDistribute.getId());
			}
		}
	}

	//删除下发-CTF题目集中间表
	@Transactional(readOnly = false)
	public void deleteDistributeCtfSetList(TeachDistribute teachDistribute) {
		//当CtfSetList不为 0 时，为修改操作，则先删除之前插入的记录，再重新插入
		TeachDistributeCtfset teachDistributeCtfset = new TeachDistributeCtfset();
		teachDistributeCtfset.setDistributeId(teachDistribute.getId());
		List<TeachDistributeCtfset> CtfSetList = teachDistributeCtfsetService.findList(teachDistributeCtfset);
		if(CtfSetList.size() != 0){
			logger.debug("==Evan=========先删除-CTF题目集======================");
			for(int i=0; i<CtfSetList.size(); i++){
				teachDistributeCtfsetDao.deleteDistributeCtfset(teachDistribute.getId());
			}
		}
	}

	//删除下发-试卷中间表
	@Transactional(readOnly = false)
	public void deleteDistributePaperList(TeachDistribute teachDistribute) {
		//当PaperList不为 0 时，为修改操作，则先删除之前插入的记录，再重新插入
		TeachDistributePaper teachDistributePaper = new TeachDistributePaper();
		teachDistributePaper.setDistributeId(teachDistribute.getId());
		List<TeachDistributePaper> PaperList = teachDistributePaperService.findList(teachDistributePaper);
		if(PaperList.size() != 0){
			logger.debug("==Evan=========先删除-试卷======================");
			for(int i = 0; i < PaperList.size(); i++){
				teachDistributePaperDao.deleteDistributePaper(teachDistribute.getId());
			}
		}
	}

	//删除考试表
	@Transactional(readOnly = false)
	public void deleteExamList(TeachDistribute teachDistribute) {
		//当ExamList不为 0 时，为修改操作，则先删除之前插入的记录，再重新插入
		TeachExam teachExam = new TeachExam();
		teachExam.setDistributeId(teachDistribute.getId());
		List<TeachExam> ExamList = teachExamService.findList(teachExam);
		if(ExamList.size() != 0){
			logger.debug("==Evan=========先删除-考试======================");
			for(int i = 0; i < ExamList.size(); i++){
				teachExamDao.deleteExam(teachDistribute.getId());
			}
		}

	}

	//删除学生-考试中间表
	@Transactional(readOnly = false)
	public void deleteStudentExamList(TeachDistribute teachDistribute) {
		//当StudentExamList不为 0 时，为修改操作，则先删除之前插入的记录，再重新插入
		TeachStudentExam teachStudentExam = new TeachStudentExam();
		teachStudentExam.setDistributeId(teachDistribute.getId());
		List<TeachStudentExam> StudentExamList = teachStudentExamService.findList(teachStudentExam);
		if(StudentExamList.size() != 0){
			logger.debug("==Evan=========先删除-学生-考试中间表======================");
			for(int i = 0; i < StudentExamList.size(); i++){
				teachStudentExamDao.deleteStudentExam(teachDistribute.getId());
			}
		}

	}

	//TODO 根据下发id找到之前下发的课程，修改课程计数表,将计数减1
	@Transactional(readOnly = false)
	public void  updateCourse (TeachDistribute teachDistribute){
		TeachDistributeStudent teachDistributeStudent = new TeachDistributeStudent();
		teachDistributeStudent.setDistributeId(teachDistribute.getId());
		List<String> distributeStudentList = teachDistributeStudentDao.findStudentListByDistributeId(teachDistributeStudent);
		logger.debug("==Evan==distributeStudentList:"+distributeStudentList);

		TeachDistributeCoursesystem teachDistributeCoursesystem = new TeachDistributeCoursesystem();
		teachDistributeCoursesystem.setDistributeId(teachDistribute.getId());
		List<String> CourseSystemList = teachDistributeCoursesystemDao.findCourseSystemListByDistributeId(teachDistributeCoursesystem);
		logger.debug("==Evan==CourseSystemList:"+CourseSystemList);

		//遍历学生id
		for (String aDistributeStudentList : distributeStudentList) {
			//遍历课程体系id
			for(String CourseSystemIdList:CourseSystemList){
				ResourceCourse resourceCourse = new ResourceCourse();
				resourceCourse.setCourseSystemId(CourseSystemIdList);
				//遍历课程id
				for(ResourceCourse CourseIdList: resourceCourseDao.findCourseListByCoursesystemId(resourceCourse)){
					TeachCourseCount teachCourseCount = new TeachCourseCount();
					teachCourseCount.setCoursesystemId(CourseSystemIdList);
					teachCourseCount.setCourseId(CourseIdList.getId());
					teachCourseCount.setUserId(aDistributeStudentList);
					teachCourseCount.setUpdateBy(UserUtils.getUser());
					teachCourseCount.setUpdateDate(new Date());

					//TODO userId、CourseSystemId、CourseId获取单条记录,判断在teachCourseCount是否已经有存在的记录
					TeachCourseCount teachCourseCount2 = teachCourseCountDao.getSingleRecord(aDistributeStudentList, CourseSystemIdList, CourseIdList.getId(), DeleteFlag.deleteFlag);
					logger.debug("===Evan===将计数减1==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  课程体系id："+CourseSystemIdList+"  课程id："+CourseIdList.getId());
					if (teachCourseCount2 == null){
						continue;
					}else {
						int getCountCourse = teachCourseCount2.getCountCourse();
						getCountCourse--;
						teachCourseCount.setCountCourse(getCountCourse);
						teachCourseCountDao.update(teachCourseCount);
					}
				}
			}

		}
	}

	//TODO 根据下发id找到之前下发的CTF题目集，修改CTF题目集计数表,将计数减1
	@Transactional(readOnly = false)
	public void  updateCtfSet (TeachDistribute teachDistribute){
		TeachDistributeStudent teachDistributeStudent = new TeachDistributeStudent();
		teachDistributeStudent.setDistributeId(teachDistribute.getId());
		List<String> distributeStudentList = teachDistributeStudentDao.findStudentListByDistributeId(teachDistributeStudent);
		logger.debug("==Evan==distributeStudentList:"+distributeStudentList);

		TeachDistributeCtfset teachDistributeCtfset = new TeachDistributeCtfset();
		teachDistributeCtfset.setDistributeId(teachDistribute.getId());
		List<String> CtfSetList = teachDistributeCtfsetDao.findCtfsetListByDistributeId(teachDistributeCtfset);
		logger.debug("==Evan==CtfSetList:"+CtfSetList);

		//遍历学生id
		for (String aDistributeStudentList : distributeStudentList) {
			//遍历课程体系id
			for(String CtfSetIdList:CtfSetList){
				//要插入teachCtfSetCount的数据
				TeachCtfsetcount teachCtfsetcount = new TeachCtfsetcount();
				teachCtfsetcount.setCtfsetId(CtfSetIdList);
				teachCtfsetcount.setUserId(aDistributeStudentList);
				teachCtfsetcount.setUpdateBy(UserUtils.getUser());
				teachCtfsetcount.setUpdateDate(new Date());

				//TODO userId、CtfsetId获取单条记录,判断在teachCtfSetCount是否已经有存在的记录
				TeachCtfsetcount teachCtfsetcount2 = teachCtfsetcountDao.getSingleRecord(aDistributeStudentList, CtfSetIdList, DeleteFlag.deleteFlag);
				logger.debug("===Evan===将计数减1==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  CTF题目集id："+CtfSetIdList);
				if (teachCtfsetcount2 == null){
					continue;
				}else {
					int getCountCtfSet = teachCtfsetcount2.getCountCtfset();
					getCountCtfSet--;
					teachCtfsetcount.setCountCtfset(getCountCtfSet);
					teachCtfsetcountDao.update(teachCtfsetcount);
				}
			}

		}
	}

	//TODO 根据下发id找到之前下发的试卷，修改试卷计数表,将计数减1
	@Transactional(readOnly = false)
	public void  updatePaper (TeachDistribute teachDistribute){
		TeachDistributeStudent teachDistributeStudent = new TeachDistributeStudent();
		teachDistributeStudent.setDistributeId(teachDistribute.getId());
		List<String> distributeStudentList = teachDistributeStudentDao.findStudentListByDistributeId(teachDistributeStudent);
		logger.debug("==Evan==distributeStudentList:"+distributeStudentList);

		TeachDistributePaper teachDistributePaper = new TeachDistributePaper();
		teachDistributePaper.setDistributeId(teachDistribute.getId());
		List<String> PaperList = teachDistributePaperDao.findPaperListByDistributeId(teachDistributePaper);
		logger.debug("==Evan==PaperList:"+PaperList);

		//遍历学生id
		for (String aDistributeStudentList : distributeStudentList) {
			//遍历课程体系id
			for(String PaperIdList:PaperList){
				//要插入teachCtfSetCount的数据
				TeachPapercount teachPapercount = new TeachPapercount();
				teachPapercount.setPaperId(PaperIdList);
				teachPapercount.setUserId(aDistributeStudentList);
				teachPapercount.setUpdateBy(UserUtils.getUser());
				teachPapercount.setUpdateDate(new Date());

				//TODO userId、PaperId获取单条记录,判断在teachPapercount是否已经有存在的记录
				TeachPapercount teachPapercount2 = teachPapercountDao.getSingleRecord(aDistributeStudentList, PaperIdList, DeleteFlag.deleteFlag);
				logger.debug("===Evan===将计数减1==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  试卷id："+PaperIdList);
				if (teachPapercount2 == null){
					continue;
				}else {
					int getCountPaper = teachPapercount2.getCountPaper();
					getCountPaper--;
					teachPapercount.setCountPaper(getCountPaper);
					teachPapercountDao.update(teachPapercount);
				}
			}

		}
	}


//	==============================================插入 and 修改==========================================================

	//添加-修改下发
	@Transactional(readOnly = false)
	public void saveDistribute(TeachDistribute teachDistribute, String[] distributeClassArray, String[] distributeStudentArray,
							   String[] CourseSystemArray, String[] CtfSetArray, String[] PaperArray){

		logger.debug("==Evan=========teachDistribute======================"+teachDistribute.getId());

		if(StringUtils.isBlank(teachDistribute.getId())) {
			logger.debug("==Evan=========New======================");
			//TODO 插入下发内容表
			//将是否下发标志位设为 0
			teachDistribute.setDistributeOrNot(DistributeOrNot.distributeNo);
			teachDistribute.preInsert();
			teachDistributeDao.insert(teachDistribute);

		}else {
			logger.debug("==Evan=========Old======================");
			//TODO 更新下发内容表
			//将是否下发标志位设为 0
			teachDistribute.setDistributeOrNot(DistributeOrNot.distributeNo);
			teachDistribute.preUpdate();
			teachDistributeDao.update(teachDistribute);

			//TODO 修改时：先对之前下发课程记录减1，再插入
			updateCourse(teachDistribute);

			//TODO 修改时：先对之前下发CTF题目集记录减1，再插入
			updateCtfSet(teachDistribute);

			//TODO 修改时：先对之前下发试卷记录减1，再插入
			updatePaper(teachDistribute);

			//TODO 修改时：先删除之前下发学生中间表的记录，再插入
			deleteDistributeStudentList(teachDistribute);

			//TODO 修改时：先删除之前下发班级中间表的记录，再插入
			deleteDistributeClassList(teachDistribute);

			//TODO 修改时：先删除之前下发课程体系中间表的记录，再插入
			deleteDistributeCourseSystemList(teachDistribute);

			//TODO 修改时：先删除之前下发CTF题目集中间表的记录，再插入
			deleteDistributeCtfSetList(teachDistribute);

			//TODO 修改时：先删除之前下发试卷中间表的记录，再插入
			deleteDistributePaperList(teachDistribute);

			//TODO 修改时：先删除之前配置考试的记录，再插入
			deleteExamList(teachDistribute);

			//TODO 修改时：先删除之前学生考试的记录，再插入
			deleteStudentExamList(teachDistribute);

		}

		//TODO 下发课程体系
		List<String> CourseSystemList = new LinkedList<>();
		if(CourseSystemArray != null){
			Collections.addAll(CourseSystemList, CourseSystemArray);
			logger.debug("==Evan==CourseSystemList:"+CourseSystemList);
			//TODO 插入下发课程体系中间表
			saveDistributeCourseSystem(teachDistribute, CourseSystemList);
		}

		//TODO 下发CTF题目集
		List<String> CtfSetList = new LinkedList<>();
		if(CtfSetArray != null){
			Collections.addAll(CtfSetList, CtfSetArray);
			logger.debug("==Evan==CtfSetList:"+CtfSetList);
			//TODO 插入下发CTF题目集中间表
			saveDistributeCtfSet(teachDistribute, CtfSetList);
		}

		//TODO 下发试卷
		List<String> PaperList = new LinkedList<>();
		if(PaperArray != null){
			Collections.addAll(PaperList, PaperArray);
			logger.debug("==Evan==PaperList:"+PaperList);
			//TODO 插入下发试卷中间表
			saveDistributePaper(teachDistribute, PaperList);
		}

		//TODO 下发对象是：学生
		if(distributeStudentArray != null){
			List<String> distributeStudentList = new LinkedList<>();
			Collections.addAll(distributeStudentList, distributeStudentArray);
			//TODO 插入下发学生中间表
			saveDistributeStudent(teachDistribute, distributeStudentList);
			//TODO 插入课程计数表
//			saveAndDuplicateCourse(teachDistribute, distributeStudentList, CourseSystemList);
			//TODO 插入CTF题目集计数表
//			saveAndDuplicateCtfSet(teachDistribute, distributeStudentList, CtfSetList);
		}

		//TODO 下发对象是：班级
		if(distributeClassArray != null){
			List<String> distributeClassList = new LinkedList<>();
			Collections.addAll(distributeClassList, distributeClassArray);
			//TODO 插入下发班级中间表
			saveDistributeClass(teachDistribute, distributeClassList);
			for (String aDistributeClassArray : distributeClassArray) {
				List<String> distributeStudentList = new LinkedList<>();
				for (int j = 0; j < teachStudentService.findStudentByClassId(aDistributeClassArray).size(); j++) {
					distributeStudentList.add(teachStudentService.findStudentByClassId(aDistributeClassArray).get(j).getId());
				}
				logger.debug("aDistributeClassArray:"+aDistributeClassArray+"  distributeStudentList:"+distributeStudentList);
				//TODO 插入下发学生中间表
				saveDistributeStudent(teachDistribute, distributeStudentList);
				//TODO 插入课程计数表
//				saveAndDuplicateCourse(teachDistribute, distributeStudentList, CourseSystemList);
				//TODO 插入CTF题目集计数表
//				saveAndDuplicateCtfSet(teachDistribute, distributeStudentList, CtfSetList);
			}
		}



	}

	//TODO 插入下发学生中间表
	@Transactional(readOnly = false)
	public void saveDistributeStudent(TeachDistribute teachDistribute, List<String> distributeStudentList){
		for (String aDistributeStudentList : distributeStudentList) {
			TeachDistributeStudent teachDistributeStudent2 = new TeachDistributeStudent();
			teachDistributeStudent2.setDistributeId(teachDistribute.getId());
			teachDistributeStudent2.setUserId(aDistributeStudentList);
			teachDistributeStudent2.preInsert();
			teachDistributeStudentDao.insert(teachDistributeStudent2);
		}
	}

	//TODO 插入下发班级中间表
	@Transactional(readOnly = false)
	public void saveDistributeClass(TeachDistribute teachDistribute, List<String> distributeClassList){
		for (String aDistributeClassList : distributeClassList) {
			TeachDistributeClass teachDistributeClass2 = new TeachDistributeClass();
			teachDistributeClass2.setDistributeId(teachDistribute.getId());
			teachDistributeClass2.setClassId(aDistributeClassList);
			teachDistributeClass2.preInsert();
			teachDistributeClassDao.insert(teachDistributeClass2);
		}
	}

	//TODO 插入下发课程体系中间表
	@Transactional(readOnly = false)
	public void saveDistributeCourseSystem(TeachDistribute teachDistribute, List<String> CourseSystemList){
		for (String aCourseSystemList : CourseSystemList){
			TeachDistributeCoursesystem teachDistributeCoursesystem2 = new TeachDistributeCoursesystem();
			teachDistributeCoursesystem2.setDistributeId(teachDistribute.getId());
			teachDistributeCoursesystem2.setCoursesystemId(aCourseSystemList);
			teachDistributeCoursesystem2.preInsert();
			teachDistributeCoursesystemDao.insert(teachDistributeCoursesystem2);
		}
	}

	//TODO 插入课程计数表，判断下发课程体系-课程是否重复下发
	@Transactional(readOnly = false)
	public void  saveAndDuplicateCourse (TeachDistribute teachDistribute, List<String> distributeStudentList, List<String> CourseSystemList){
		//遍历学生id
		for (String aDistributeStudentList : distributeStudentList) {
			//遍历课程体系id
			for(String CourseSystemIdList:CourseSystemList){
				ResourceCourse resourceCourse = new ResourceCourse();
				resourceCourse.setCourseSystemId(CourseSystemIdList);
				//遍历课程id
				for(ResourceCourse CourseIdList: resourceCourseDao.findCourseListByCoursesystemId(resourceCourse)){
					//要插入teachCourseCount的数据
					TeachCourseCount teachCourseCount = new TeachCourseCount();
					teachCourseCount.setCoursesystemId(CourseSystemIdList);
					teachCourseCount.setCourseId(CourseIdList.getId());
					teachCourseCount.setUserId(aDistributeStudentList);
					teachCourseCount.setUpdateBy(UserUtils.getUser());
					teachCourseCount.setUpdateDate(new Date());

					//TODO userId、CourseSystemId、CourseId获取单条记录,判断在teachCourseCount是否已经有存在的记录
					TeachCourseCount teachCourseCount2 = teachCourseCountDao.getSingleRecord(aDistributeStudentList, CourseSystemIdList, CourseIdList.getId(), DeleteFlag.deleteFlag);
					if(teachCourseCount2 == null){
						logger.debug("===Evan===第一次插入==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  课程体系id："+CourseSystemIdList+"  课程id："+CourseIdList.getId());
						teachCourseCount.setCountCourse(1);
						teachCourseCount.preInsert();
						teachCourseCountDao.insert(teachCourseCount);
					}else{
						logger.debug("===Evan===第一之后的次插入==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  课程体系id："+CourseSystemIdList+"  课程id："+CourseIdList.getId());
						int getCountCourse = teachCourseCount2.getCountCourse();
						getCountCourse++;
						teachCourseCount.setCountCourse(getCountCourse);
						teachCourseCountDao.update(teachCourseCount);
					}
				}

			}

		}
	}

	//TODO 插入下发CTF题目集中间表
	@Transactional(readOnly = false)
	public void saveDistributeCtfSet(TeachDistribute teachDistribute, List<String> CtfSetList){
		for (String aCtfSetList : CtfSetList) {
			TeachDistributeCtfset teachDistributeCtfset = new TeachDistributeCtfset();
			teachDistributeCtfset.setDistributeId(teachDistribute.getId());
			teachDistributeCtfset.setCtfsetId(aCtfSetList);
			teachDistributeCtfset.setSolutionIsAllow(null);
			teachDistributeCtfset.preInsert();
			teachDistributeCtfsetDao.insert(teachDistributeCtfset);
		}
	}

	//TODO 插入CTF题目集计数表，判断下发CTF题目集是否重复下发
	@Transactional(readOnly = false)
	public void  saveAndDuplicateCtfSet (TeachDistribute teachDistribute, List<String> distributeStudentList, List<String> CtfSetList){
		//遍历学生id
		for (String aDistributeStudentList : distributeStudentList) {
			//遍历CTF题目集id
			for(String CtfSetIdList:CtfSetList){
				//要插入teachCtfSetCount的数据
				TeachCtfsetcount teachCtfsetcount = new TeachCtfsetcount();
				teachCtfsetcount.setCtfsetId(CtfSetIdList);
				teachCtfsetcount.setUserId(aDistributeStudentList);
				teachCtfsetcount.setUpdateBy(UserUtils.getUser());
				teachCtfsetcount.setUpdateDate(new Date());

				//TODO userId、CtfsetId获取单条记录,判断在teachCtfSetCount是否已经有存在的记录
				TeachCtfsetcount teachCtfsetcount2 = teachCtfsetcountDao.getSingleRecord(aDistributeStudentList, CtfSetIdList, DeleteFlag.deleteFlag);
				if(teachCtfsetcount2 == null){
					logger.debug("===Evan===第一次插入==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  CTF题目集id："+CtfSetIdList);
					teachCtfsetcount.setCountCtfset(1);
					teachCtfsetcount.preInsert();
					teachCtfsetcountDao.insert(teachCtfsetcount);
				}else{
					logger.debug("===Evan===第一之后的次插入==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  CTF题目集id："+CtfSetIdList);
					int getCountCtfSet = teachCtfsetcount2.getCountCtfset();
					getCountCtfSet++;
					teachCtfsetcount.setCountCtfset(getCountCtfSet);
					teachCtfsetcountDao.update(teachCtfsetcount);
				}
			}
		}
	}

	//TODO 插入下发试卷中间表
	@Transactional(readOnly = false)
	public void saveDistributePaper(TeachDistribute teachDistribute, List<String> PaperList){
		for (String aPaperList : PaperList) {
			TeachDistributePaper teachDistributePaper = new TeachDistributePaper();
			teachDistributePaper.setDistributeId(teachDistribute.getId());
			teachDistributePaper.setPaperId(aPaperList);
			teachDistributePaper.preInsert();
			teachDistributePaperDao.insert(teachDistributePaper);
		}
	}

	//TODO 插入试卷计数表，判断下发试卷是否重复下发
	@Transactional(readOnly = false)
	public void  saveAndDuplicatePaper (TeachDistribute teachDistribute, List<String> distributeStudentList, List<String> PaperList){
		//遍历学生id
		for (String aDistributeStudentList : distributeStudentList) {
			//遍历试卷id
			for(String PaperIdList:PaperList){
				//要插入teachPapercount的数据
				TeachPapercount teachPapercount = new TeachPapercount();
				teachPapercount.setPaperId(PaperIdList);
				teachPapercount.setUserId(aDistributeStudentList);
				teachPapercount.setUpdateBy(UserUtils.getUser());
				teachPapercount.setUpdateDate(new Date());

				//TODO userId、PaperId获取单条记录,判断在teachPapercount是否已经有存在的记录
				TeachPapercount teachPapercount2 = teachPapercountDao.getSingleRecord(aDistributeStudentList, PaperIdList, DeleteFlag.deleteFlag);
				if(teachPapercount2 == null){
					logger.debug("===Evan===第一次插入==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  试卷id："+PaperIdList);
					teachPapercount.setCountPaper(1);
					teachPapercount.preInsert();
					teachPapercountDao.insert(teachPapercount);
				}else{
					logger.debug("===Evan===第一之后的次插入==下发id："+teachDistribute.getId()+"  学生id："+aDistributeStudentList+"  试卷id："+PaperIdList);
					int getCountPaper = teachPapercount2.getCountPaper();
					getCountPaper++;
					teachPapercount.setCountPaper(getCountPaper);
					teachPapercountDao.update(teachPapercount);
				}
			}
		}
	}

}